package com.ruoyi.contract.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.contract.domain.ContractCategory;
import com.ruoyi.contract.domain.contractVo.ContractCategoryVo;
import com.ruoyi.contract.mapper.ContractCategoryMapper;
import com.ruoyi.contract.tools.ChineseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.contract.mapper.BelongingCompanyMapper;
import com.ruoyi.contract.domain.BelongingCompany;
import com.ruoyi.contract.service.IBelongingCompanyService;

/**
 * 合同所属公司Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-18
 */
@Service
public class BelongingCompanyServiceImpl implements IBelongingCompanyService 
{
    @Autowired
    private BelongingCompanyMapper belongingCompanyMapper;

    @Autowired
    private ContractCategoryMapper contractCategoryMapper;

    /**
     * 查询合同所属公司
     * 
     * @param belongingCompanyId 合同所属公司主键
     * @return 合同所属公司
     */
    @Override
    public BelongingCompany selectBelongingCompanyByBelongingCompanyId(Long belongingCompanyId)
    {
        return belongingCompanyMapper.selectBelongingCompanyByBelongingCompanyId(belongingCompanyId);
    }

    /**
     * 查询合同所属公司列表
     * 
     * @param belongingCompany 合同所属公司
     * @return 合同所属公司
     */
    @Override
    public List<BelongingCompany> selectBelongingCompanyList(BelongingCompany belongingCompany) {
        //获取合同所属公司列表
        List<BelongingCompany> belongingCompanyList = belongingCompanyMapper.selectBelongingCompanyList(belongingCompany);
        for (BelongingCompany companyInfo : belongingCompanyList) {
            //根据合同所属公司id获取合同类别信息
            List<ContractCategory> contractCategoryList = contractCategoryMapper.
                    selectContractCategoryByBelongingCompanyId(companyInfo.getBelongingCompanyId());
            //初始化
            List<ContractCategoryVo> categoryList = new ArrayList<>(contractCategoryList.size());

            for (ContractCategory contractCategory : contractCategoryList) {
                ContractCategoryVo newContractCategory = new ContractCategoryVo();
                //将返回信息复制
                BeanUtils.copyProperties(contractCategory, newContractCategory);
                categoryList.add(newContractCategory);
            }
            companyInfo.setContractCategoryList(categoryList);
        }
        return belongingCompanyList;
    }


    /**
     * 新增合同所属公司
     * 
     * @param belongingCompany 合同所属公司
     * @return 结果
     */
    @Override
    public int insertBelongingCompany(BelongingCompany belongingCompany)
    {
        // 校验名称长度是否符合要求
        if (belongingCompany.getName() == null || belongingCompany.getName().length() > 50) {
            throw new IllegalArgumentException("合同公司名称长度应小于等于50");
        }
        String companyCode = ChineseUtil.getFirstLetter(belongingCompany.getName());
        belongingCompany.setCode(companyCode);
        return belongingCompanyMapper.insertBelongingCompany(belongingCompany);
    }

    /**
     * 修改合同所属公司
     * 
     * @param belongingCompany 合同所属公司
     * @return 结果
     */
    @Override
    public int updateBelongingCompany(BelongingCompany belongingCompany)
    {
        String companyCode = ChineseUtil.getFirstLetter(belongingCompany.getName());
        belongingCompany.setCode(companyCode);
        return belongingCompanyMapper.updateBelongingCompany(belongingCompany);
    }

    /**
     * 批量删除合同所属公司
     * 
     * @param belongingCompanyIds 需要删除的合同所属公司主键
     * @return 结果
     */
    @Override
    public int deleteBelongingCompanyByBelongingCompanyIds(Long[] belongingCompanyIds)
    {
        return belongingCompanyMapper.deleteBelongingCompanyByBelongingCompanyIds(belongingCompanyIds);
    }

    /**
     * 删除合同所属公司信息
     * 
     * @param belongingCompanyId 合同所属公司主键
     * @return 结果
     */
    @Override
    public int deleteBelongingCompanyByBelongingCompanyId(Long belongingCompanyId)
    {
        return belongingCompanyMapper.deleteBelongingCompanyByBelongingCompanyId(belongingCompanyId);
    }
}
